/* 
 *  Created By: Mike Stevens
 *  Project URL: http://www.codeplex.com/sitesecuritymgmt
 *  License: GNU GPL v2
 *  Copyright (C) 2008 Mike Stevens
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using SiteSecurityManagement.Service;
using System.Web.UI.WebControls;
using SiteSecurityManagement.Entities.SecurityTree;
using SiteSecurityManagement.Entities.SharePoint;

namespace SiteSecurityManagement.Service.SecurityTree
{
    class SecurityTreeUtility
    {
        #region Members
        private string[] m_siteStack;
        private char m_pathSeperator=';';
        #endregion

        #region Constructor/Destructor
        public SecurityTreeUtility(TreeNode node, char pathSeperator)
        {
            m_siteStack = node.ValuePath.Split(new char[]{pathSeperator});
        }
        #endregion

        #region Properties
        
        #endregion

        #region Methods
        /// <summary>
        /// Gets the object types.
        /// </summary>
        /// <param name="nodes">The nodes.</param>
        /// <param name="pathSeperator">The path seperator.</param>
        /// <returns></returns>
        public SecurityTreeNodeTypes[] GetObjectTypes(TreeNodeCollection nodes, char pathSeperator)
        {
            List<SecurityTreeNodeTypes> list = new List<SecurityTreeNodeTypes>();
            foreach (TreeNode node in nodes)
            {
                m_siteStack=node.ValuePath.Split(new char[]{pathSeperator});
                list.Add(GetObjectType());
            }
            return list.ToArray();
        }

        /// <summary>
        /// Gets the type of the object.
        /// </summary>
        /// <returns></returns>
        public SecurityTreeNodeTypes GetObjectType()
        {
            string listItemType = Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPListItem);
            string picLibType = Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPPictureLibrary);
            string fileType = Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPFile);
            string folderType = Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPFolder);
            string docLibType = Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPDocumentLibrary);
            string listType = Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPList);
            string siteType = Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPWeb);

            //Break out node values into string array
            if (m_siteStack.Length>=2)
            {
                string spObject=m_siteStack[m_siteStack.Length-2];
                //determine leaf type selected for object creation
                //assumes node value of Type;Guid
                if (true == spObject.Contains(folderType))
                {
                    return SecurityTreeNodeTypes.SPFolder;
                }
                else if (true == spObject.Contains(fileType))
                {
                    return SecurityTreeNodeTypes.SPFile;
                }
                else if (true == spObject.Contains(listItemType))
                {
                    return SecurityTreeNodeTypes.SPListItem;
                }
                else if (true == spObject.Contains(docLibType))
                {
                    return SecurityTreeNodeTypes.SPDocumentLibrary;
                }
                else if (true == spObject.Contains(listType))
                {
                    return SecurityTreeNodeTypes.SPList;
                }
                else if (true == spObject.Contains(picLibType))
                {
                    return SecurityTreeNodeTypes.SPPictureLibrary;
                }
                else if (true == spObject.Contains(siteType))
                {
                    return SecurityTreeNodeTypes.SPWeb;
                }
                else
                {
                    return SecurityTreeNodeTypes.InvalidObjectType;
                }
            }
            return SecurityTreeNodeTypes.InvalidObjectType;
        }

        /// <summary>
        /// Gets the file.
        /// </summary>
        /// <returns></returns>
        public SPFile GetFile()
        {
            string listItemID = GetFileGuid();
            SPDocumentLibrary list = GetDocumentLibrary();
            return SPFileObject.GetFile(list, new Guid(listItemID));
        }

        /// <summary>
        /// Gets the list item.
        /// </summary>
        /// <returns></returns>
        public SPListItem GetListItem()
        {
            string listItemID = GetListItemGuid();
            SPList list = GetList();
            return SPListItemObject.GetListItem(list, new Guid(listItemID));
        }

        /// <summary>
        /// Gets the folder.
        /// </summary>
        /// <returns></returns>
        public SPFolder GetFolder()
        {
            string folderID = GetFolderGuid();
            SPDocumentLibrary docLib = GetDocumentLibrary();
            return SPFolderObject.GetFolder(docLib, new Guid(folderID));
        }

        /// <summary>
        /// Gets the picture library.
        /// </summary>
        /// <returns></returns>
        public SPPictureLibrary GetPictureLibrary()
        {
            return (SPPictureLibrary)GetList(Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPPictureLibrary));
        }

        /// <summary>
        /// Gets the document library.
        /// </summary>
        /// <returns></returns>
        public SPDocumentLibrary GetDocumentLibrary()
        {
            return (SPDocumentLibrary)GetList(Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPDocumentLibrary));
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <returns></returns>
        public SPList GetList()
        {
            return GetList("SPList");
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="listType">Type of the list.</param>
        /// <returns></returns>
        protected SPList GetList(string listType)
        {
            string listID = GetListGuid(listType);

            using (SPWeb site = GetSite())
            {
                return SPListObject.GetList(site, new Guid(listID));
            }
        }

        /// <summary>
        /// Gets the site.
        /// </summary>
        /// <returns></returns>
        public SPWeb GetSite()
        {
            string siteCollectionID = GetSiteCollectionGuid();
            string siteID = GetSiteGuid();

            return SPSiteObject.GetSite(new Guid(siteCollectionID), new Guid(siteID));
        }

        /// <summary>
        /// Gets the site GUID.
        /// </summary>
        /// <returns></returns>
        protected string GetSiteGuid()
        {
            return FindObjectIDByType(Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPWeb));
        }

        /// <summary>
        /// Gets the site collection GUID.
        /// </summary>
        /// <returns></returns>
        protected string GetSiteCollectionGuid()
        {
            return FindObjectIDByType(Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPSite));
        }

        /// <summary>
        /// Gets the list GUID.
        /// </summary>
        /// <param name="listType">Type of the list.</param>
        /// <returns></returns>
        protected string GetListGuid(string listType)
        {
            return FindObjectIDByType(listType);
        }

        /// <summary>
        /// Gets the folder GUID.
        /// </summary>
        /// <returns></returns>
        protected string GetFolderGuid()
        {
            return FindObjectIDByType(Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPFolder));
        }

        /// <summary>
        /// Gets the file GUID.
        /// </summary>
        /// <returns></returns>
        protected string GetFileGuid()
        {
            return FindObjectIDByType(Enum.GetName(typeof(SecurityTreeNodeTypes), SecurityTreeNodeTypes.SPFile));
        }

        /// <summary>
        /// Gets the list item GUID.
        /// </summary>
        /// <returns></returns>
        protected string GetListItemGuid()
        {
            return FindObjectIDByType(Enum.GetName(typeof(SecurityTreeNodeTypes),SecurityTreeNodeTypes.SPListItem));
        }

        /// <summary>
        /// Finds the object ID by type.
        /// </summary>
        /// <param name="findType">object type.</param>
        /// <returns></returns>
        protected string FindObjectIDByType(string findType)
        {
            try
            {
                for (int i = m_siteStack.Length - 1; i >= 0; i--)
                {
                    string spObject = m_siteStack[i];
                    if (true == spObject.EndsWith(findType))
                    {
                        return m_siteStack[i+1];
                    }
                }
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        #endregion
    }
}
